
from db import db1

class ACL:
    def __init__(self):
        pass
    # 添加权限
    def set_resource(self,uid,reslist):
        for i in reslist:
            sql = "insert into user_res values(%d,%d)"%(int(uid),int(i))
            db1.update(sql)
    # 修改权限
    def update_resource(self,uid,reslist):
        sql = "delete from user_res where userid=%d "%(int(uid))
        db1.update(sql)
        self.set_resource(uid,reslist)

    # 删除单个权限
    def delete_resource(self,uid,resourceid):
        sql = "delete from user_res where userid=%d and resourceid=%d"%(int(uid),int(resourceid))
        db1.update(sql)
    # 获取页面资源列表   1 为前端页面资源
    def get_pagedef_reslist(self,uid):
        sql = "select r.url as url from user_res as u inner join resource as r on u.resourceid=r.id where u.userid=%d and r.type=1"%(int(uid))
        res = db1.find_all(sql)
        lst = []
        for i in res:
            lst.append(i['url'])
        return lst
#     获取接口资源列表’ 2 为后台资源
    def get_interface_reslist(self,uid):
        sql = "select r.url as url from user_res as u inner join resource as r on u.resourceid=r.id where u.userid=%d and r.type=2" % (int(uid))
        res = db1.find_all(sql)
        lst = []
        for i in res:
            lst.append(i['url'])
        return lst

    # 获取页面资源列表   1 为前端页面资源
    def get_pagedelist(self):
        sql = "select r.id,r.name,r.url as url from  resource as r "
        res = db1.find_all(sql)
        return res



acl = ACL()